package controller;


import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;



import net.tanesha.recaptcha.ReCaptcha;
import net.tanesha.recaptcha.ReCaptchaFactory;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.support.ServletContextResource;

import pojo.Status;
import pojo.User;

import dao.StatusDAO;
import dao.UserDAO;


@Controller
public class TaiKhoanController {

	static private String tentk="";
	static private String hotentk="";
	static private String diachitk="";
	static private int kieuSort=1;
	static private int slusertt=-1;
	static private int page=-1;
	
	@RequestMapping (value = "ThemTaiKhoan.do", method = RequestMethod.GET)
	public String themtkget(
			Model model,
			HttpServletRequest request)
	{
    	HttpSession session = request.getSession();
    	User us = (User) session
				.getAttribute("user");
    	if(us==null)       return ("page/FrontEnd/DangNhap");
    	if(us.getIsAdmin()==0) return ("page/FrontEnd/DangNhap");
		ReCaptcha c = ReCaptchaFactory.newReCaptcha(
				"6Ldjn9oSAAAAAK2FKcLUewjwChpB_3xmCcnByDYp",
				"6Ldjn9oSAAAAAGsu7CZ0AZVFvIsnhSFqyaM6uEU2", false);
		request.setAttribute("captchar", c);
		model.addAttribute("title1","Thêm Tài Khoản");
		return "page/BackEnd/ThemTaiKhoan";
	}	
	@RequestMapping(value = "ThemTaiKhoan.do", method = RequestMethod.POST)
	public @ResponseBody String show(
			@RequestParam(value = "HoVaTen", required = false) String hoTen,
			@RequestParam(value = "Ngay", required = false) String ngay,
			@RequestParam(value = "Thang", required = false) String thang,
			@RequestParam(value = "Nam", required = false) String nam,
			@RequestParam(value = "DiaChi", required = false) String diaChi,
			@RequestParam(value = "Email", required = false) String email,
			@RequestParam(value = "DienThoai", required = false) String dienThoai,
			@RequestParam(value = "TenDangNhap", required = false) String tenDangNhap,
			@RequestParam(value = "MatKhau", required = false) String matKhau,
			HttpServletRequest request) {
		Boolean insertSuccess = true;
		UserDAO khDAO = new UserDAO();

    	HttpSession session = request.getSession();
    	User us = (User) session
				.getAttribute("user");
    	if(us==null)       return ("page/FrontEnd/DangNhap");
    	if(us.getIsAdmin()==0) return ("page/FrontEnd/DangNhap");
		insertSuccess = khDAO.insertUser(hoTen, ngay, thang, nam,
				diaChi, email, dienThoai, tenDangNhap, matKhau);
		if (insertSuccess) {
			return "success";
		} else {
			return "fail";
		}
	}
	@RequestMapping (value = "TimKiemTaiKhoan.do", method = RequestMethod.GET)
	public String TimKiemlspget(
			@RequestParam(value="Batdau", required = false)String batdau,
			@RequestParam(value = "trang", required = false) String trang,
			@RequestParam(value = "slusertt", required = false) String _slusertt,
			@RequestParam(value="tenDangNhap" ,required = false)String tenDN,
			@RequestParam(value="hoVaTen" ,required = false)String hoten,
			@RequestParam(value="diaChi" ,required = false)String diachi,
			@RequestParam(value ="save", required = false) String save,
			@RequestParam(value ="kieuSort", required = false) String _kieuSort,
			HttpServletRequest request
			)
	{
    	HttpSession session = request.getSession();
    	User us = (User) session
				.getAttribute("user");
    	if(us==null)       return ("page/FrontEnd/DangNhap");
    	if(us.getIsAdmin()==0) return ("page/FrontEnd/DangNhap");
		
		if(batdau!= null)
		return "page/BackEnd/TimKiemTaiKhoan";
		if(save.equals("false"))
		{

			 tentk="";
			 hotentk="";
			 diachitk="";
			 kieuSort=1;
			slusertt=-1;
			page=-1;
		}
		if(tenDN!=null) tentk=tenDN;
		if(hoten!=null) hotentk=tenDN;
		if(diachi!=null) diachitk=diachi;
		if(trang!=null) page=Integer.parseInt(trang);
		if(_slusertt!=null) slusertt=Integer.parseInt(_slusertt);
		if(_kieuSort!=null) kieuSort=Integer.parseInt(_kieuSort);

		UserDAO dao= new UserDAO();


		
		long tongSoTrang = dao.tinhSoTrangTimKiem(tentk, hotentk, diachitk,slusertt);
		
		List<User> llsp= dao.timKiemUser(tentk,hotentk,diachitk,page,slusertt,kieuSort);
		long ketQua = dao.tinhKetQuaTimKiem(tentk, hotentk, diachitk,slusertt);
		String kq="Có "+Long.toString(ketQua)+" kết quả";
		request.setAttribute("ketqua", kq);	
		request.setAttribute("listTaiKhoan", llsp);		
		request.setAttribute("tongSoTrang", tongSoTrang);
		request.setAttribute("slusertt",slusertt);
		request.setAttribute("Servlet", "TimKiemTaiKhoan.do?save=true");
		request.setAttribute("trangHienTai", page);
		request.setAttribute("title", "Tìm kiếm khoản khách hàng");
		request.setAttribute("timkiem","true");
		request.setAttribute("kieuSort", kieuSort);
		String st2= request.getRequestURL().toString();
    	String oldUrl=st2+"?&trang="+Integer.toString(page)+"&slusertt="+Integer.toString(slusertt)+"&kieuSort="+Integer.toString(kieuSort)+"&save=true";
    	session.setAttribute("oldlink", oldUrl);
		return "page/BackEnd/LietKeTaiKhoan";
	}
	
	@RequestMapping (value = "TimKiemTaiKhoan.do", method = RequestMethod.POST)
	public String TimKiemtkPost(
			@RequestParam(value = "trang", required = false) String trang,
			@RequestParam(value = "slusertt", required = false) String _slusertt,
			@RequestParam(value="tenDangNhap" ,required = false)String tenDN,
			@RequestParam(value="hoVaTen" ,required = false)String hoten,
			@RequestParam(value="diaChi" ,required = false)String diachi,
			@RequestParam(value ="save", required = false) String save,
			@RequestParam(value ="kieuSort", required = false) String _kieuSort,
			HttpServletRequest request
			)
	{
    	HttpSession session = request.getSession();
    	User us = (User) session
				.getAttribute("user");
    	if(us==null)       return ("page/FrontEnd/DangNhap");
    	if(us.getIsAdmin()==0) return ("page/FrontEnd/DangNhap");
		if(save.equals("false"))
		{

			 tentk="";
			 hotentk="";
			 diachitk="";
			 kieuSort=1;
			slusertt=-1;
			page=-1;
		}
		if(tenDN!=null) tentk=tenDN;
		if(hoten!=null) hotentk=tenDN;
		if(diachi!=null) diachitk=diachi;
		if(trang!=null) page=Integer.parseInt(trang);
		if(_slusertt!=null) slusertt=Integer.parseInt(_slusertt);
		if(_kieuSort!=null) kieuSort=Integer.parseInt(_kieuSort);

		UserDAO dao= new UserDAO();
		List<User> llsp= dao.timKiemUser(tentk,hotentk,diachitk,page,slusertt,kieuSort);

		long tongSoTrang = dao.tinhSoTrangTimKiem(tentk, hotentk, diachitk, slusertt);
		long ketQua = dao.tinhKetQuaTimKiem(tentk, hotentk, diachitk,slusertt);
		String kq="Có "+Long.toString(ketQua)+" kết quả";
		request.setAttribute("ketqua", kq);	
		request.setAttribute("listTaiKhoan", llsp);		
		request.setAttribute("tongSoTrang", tongSoTrang);
		request.setAttribute("slusertt", slusertt);
		request.setAttribute("Servlet", "TimKiemTaiKhoan.do?save=true");
		request.setAttribute("trangHienTai", page);
		request.setAttribute("title", "Tìm kiếm khoản khách hàng");
		request.setAttribute("kieuSort", kieuSort);
		String st2= request.getRequestURL().toString();
    	String oldUrl=st2+"?&trang="+Integer.toString(page)+"&slusertt="+Integer.toString(slusertt)+"&kieuSort="+Integer.toString(kieuSort)+"&save=true";
    	session.setAttribute("oldlink", oldUrl);
		return "page/BackEnd/LietKeTaiKhoan";
	}
	
	@RequestMapping (value = "XoaTaiKhoan.do", method = RequestMethod.GET)
	public String TimKiemtkPost(
			@RequestParam(value = "userId", required = false) String id,
			HttpServletRequest request
			)
	{
		HttpSession session = request.getSession();
    	User use = (User) session
				.getAttribute("user");
    	if(use==null)       return ("page/FrontEnd/DangNhap");
    	if(use.getIsAdmin()==0) return ("page/FrontEnd/DangNhap");
		UserDAO dao= new UserDAO();
		User us= dao.getUser(id);
		String link=(String)session.getAttribute("oldlink");
		String url="admin";
		if(link!=null) url=link;
		if(us==null)
		{
			request.setAttribute("output","Xóa thất bại");	    	
	    	request.setAttribute("link",url);
			return "template/ketqua";
		}
		boolean flag= dao.delete(us);
		if(flag==true)
		{
			request.setAttribute("output","Xóa thành công");
			request.setAttribute("link",url);
		}
		else
		{
			request.setAttribute("output","Xóa thất bại");	    	
	    	request.setAttribute("link",url);
		}
		return "template/ketqua";
	}
	@RequestMapping (value = "BanTaiKhoan.do", method = RequestMethod.GET)
	public String BanTK(
			@RequestParam(value = "userId", required = false) String id,
			HttpServletRequest request
			)
	{
		HttpSession session = request.getSession();
    	User use = (User) session
				.getAttribute("user");
    	if(use==null)       return ("page/FrontEnd/DangNhap");
    	if(use.getIsAdmin()==0) return ("page/FrontEnd/DangNhap");
		UserDAO dao= new UserDAO();
		User us= dao.getUser(id);
		String link=(String)session.getAttribute("oldlink");
		String url="admin";
		if(link!=null) url=link;
		if(us==null)
		{
			request.setAttribute("output","Ban thất bại");	    	
	    	request.setAttribute("link",url);
			return "template/ketqua";
		}
		String hd=null;
		Status tt= us.getStatus();
		StatusDAO stdao= new StatusDAO();
		if(tt.getIdstatus()==5)
		{
			hd="UnBan ";
			Status st= stdao.getStatusByID(4);
			us.setStatus(st);
		}
		else
		{
			Status st= stdao.getStatusByID(5);
			us.setStatus(st);
			hd="Ban ";
		}
		boolean flag=dao.capNhatThongTinUser(us);
		String kq;
		if(flag==true)
		{
			kq="thành công";
			//request.setAttribute("link",url);
			String redi="redirect:"+url;
			return redi;
		}
		else
		{
			kq="thất bại";
	    	request.setAttribute("link",url);
		}
		request.setAttribute("output",hd+kq);
		return "template/ketqua";
	}
	
}